//
//  Home.swift
//  XXIOT
//
//  Created by xx on 2024/8/19.
//

import UIKit
import FastSwiftSDK

class HomeViewController: FastViewController,UICollectionViewDelegate,UICollectionViewDataSource {
  
    var list:Array<Dictionary<String,Any>> = []
    private lazy var btn_add : FastButton = {
        let btn =  FastButton.init(title: "WiFi添加", imgName: "",color: themeColor ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.border(color: themeColor, width: 1)
        btn.click = { [weak self] in
            self?.add()
        }
        btn.isHidden = true
        self.view.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_bind : FastButton = {
        let btn =  FastButton.init(title: "绑定", imgName: "",color: themeColor ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.border(color: themeColor, width: 1)
        btn.click = { [weak self] in
            self?.bind()
        }
        btn.isHidden = true
        self.view.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_addCustom: FastButton = {
        let btn =  FastButton.init(title: "手动", imgName: "",color: themeColor ,font:UIFont.system(15));
        btn.cornerRadius = 6
        btn.border(color: themeColor, width: 1)
        btn.click = { [weak self] in
            self?.addCustom()
        }
        btn.isHidden = true
        self.view.addSubview(btn)
        return  btn
    } ()
    
    private lazy var collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (kScreenW-50)/2, height: (kScreenW-50)/2*1.2)
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 10
        layout.sectionInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
        let view = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        view.backgroundColor = bgColor
        view.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "homeCell")
        view.dataSource = self
        view.delegate = self
        self.view.addSubview(view)
        return  view
    } ()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        
        let lbl = FastLabel.init(frame: kFrame(0, 0, 100, 30))
        lbl.text = "测试"
        self.view.addSubview(lbl)
        
//        GPSManager().test()
        let radarView = RadarView(frame: CGRect(x: 200, y: 0, width: 300, height: 300))
                self.view.addSubview(radarView)
        radarView.cornerRadius = 150;
                
//        radarView.radarData = [
//            (angle: 30, distance: 100),
//            (angle: 60, distance: 150),
//            (angle: 90, distance: 200),
//            (angle: 120, distance: 180),
//            (angle: 180, distance: 160)
//        ]
        
//        var data =
        
        radarView.radarData = [
            (angle: 10, distance: 0),
            (angle: 15, distance: 0),
            (angle: 20, distance: 0),
            (angle: 25, distance: 0),
            (angle: 30, distance: 0),
            (angle: 35, distance: 0),
            (angle: 40, distance: 50),
            (angle: 45, distance: 50),
            (angle: 50, distance: 50),
            (angle: 55, distance: 130),
            (angle: 120, distance: 10),
            (angle: 170, distance: 0)
        ]
        
        
        Timer.scheduledTimer(withTimeInterval: 3, repeats: true) { timer in
            // 模拟数据（角度和距离），从0度到180度
            radarView.radarData = [
                (angle: 10, distance: 0),
                (angle: 15, distance: 0),
                (angle: 20, distance: 0),
                (angle: 25, distance: 0),
                (angle: 30, distance: 0),
                (angle: 35, distance: 0),
                (angle: 40, distance: 0),
                (angle: 45, distance: 0),
                (angle: 50, distance: 0),
                (angle: 55, distance: 130),
                (angle: 120, distance: 10),
                (angle: 170, distance: 0)
            ]
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(true, animated: false)
//        self.getList()
    }
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
//        collectionView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
//        }
//        btn_add.snp.makeConstraints { make in
//            make.center.equalToSuperview()
//            make.height.equalTo(btnHeigth)
//            make.width.equalTo(120)
//        }
//        btn_bind.snp.makeConstraints { make in
//            make.top.equalTo(btn_add.snp.bottom).offset(20)
//            make.centerX.width.height.equalTo(btn_add)
//        }
//        btn_addCustom.snp.makeConstraints { make in
//            make.top.equalTo(btn_bind.snp.bottom).offset(20)
//            make.centerX.width.height.equalTo(btn_add)
//        }
    }
    
    // MARK: Action
    func add(){
        FastLog.debug("add")
        self.push(AddDeviceViewController())
    }
    func bind(){
        
    }
    func addCustom(){
        FastLog.debug("addCustom")
        self.push(PlayViewController())
    }
    func createContentView(index:Int) ->FastView {
        let data = list[index]
        if data.stringConversion("type") == "RELAY"{
            let contentView = RelayView()
            contentView.set(with: data)
            return contentView
        }else{
            let contentView = FastView()
            return contentView
        }
    }
    
    // MARK: Action
    func getList(){
        NetWorkManager.shared.getDeviceList(data:["userId":userId]) { [weak self] dic in
            self?.list = dic.value(Array<Dictionary<String,Any>>.self, "list") ?? []
            if self?.list.count == 0{
                self?.btn_add.isHidden = false
                self?.btn_bind.isHidden = false
                self?.btn_addCustom.isHidden = false
                self?.collectionView.isHidden = true
            }else{
                self?.btn_add.isHidden = true
                self?.btn_bind.isHidden = true
                self?.btn_addCustom.isHidden = true
                self?.collectionView.isHidden = false
                self?.collectionView.reloadData()
            }
        } fai: { [weak self] str in
            FastHUD.showErr(str)
            self?.btn_add.isHidden = false
            self?.btn_bind.isHidden = false
            self?.btn_addCustom.isHidden = false
            self?.collectionView.isHidden = true
        }
    }
    
    // MARK: collectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.list.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "homeCell", for: indexPath)
        cell.backgroundColor = .white // 设置单元格背景颜色
        cell.cornerRadius = 6
        let contentView = createContentView(index: indexPath.item)
        cell.contentView.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = DeviceDetailViewController()
        vc.data = list[indexPath.item]
        self.push(vc)
    }

}
